<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <!-- 搜索栏-->
      <div v-if="crud.props.searchToggle">
        <el-row :gutter="10">
          <el-col :xs="24" :sm="9" :md="8">
            <treeselect
              v-model="query.dictGroupId"
              :options="dictGroupTree"
              :limit="2"
              placeholder="可根据分组名称搜索"
              no-results-text="未找到匹配项"
              no-options-text="没有可用字典分组"
              :normalizer="normalizer"
              :default-expand-level="1"
            />
          </el-col>
          <el-col :xs="24" :sm="9" :md="8">
            <el-input v-model="query.title" clearable size="small" placeholder="输入组织名称搜索" class="filter-item" @keyup.enter.native="crud.toQuery" />
          </el-col>
          <el-col :xs="24" :sm="6" :md="8">
            <rrOperation />
          </el-col>
        </el-row>

      </div>
      <!-- 按钮-->
      <crud-opts :permission="permission" />
    </div>
    <!-- 表格-->
    <el-table
      ref="table"
      v-loading="crud.loading"
      :data="crud.data"
      border
      style="width: 100% "
      row-key="id"
      stripe
      tooltip-effect="dark"
      :highlight-current-row="true"
      @sort-change="crud.sortChange"
      @selection-change="crud.selectionChangeHandler"
      @current-change="handleCurrentChange"
    >
      <el-table-column type="selection" width="39" />
      <!--<el-table-column type="index" width="45" />-->
      <el-table-column prop="value" label="字典名称" header-align="center" sortable="custom" :show-overflow-tooltip="true" />
      <el-table-column prop="dictLabel" label="字典标识" header-align="center" sortable="custom" />
      <el-table-column prop="dictGroupName" label="所属分组" :show-overflow-tooltip="true" />
      <el-table-column prop="paixu" label="排序" width="60" header-align="center" align="center" sortable="custom" />
      <!-- <el-table-column prop="depId" label="部门id" /> -->
      <el-table-column
        prop="status"
        label="状态"
        width="75"
        header-align="center"
      >
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.status"
            class="switch"
            :width="50"
            :active-value="1"
            :inactive-value="0"
            active-color="#13ce66"
            inactive-color="#ff4949"
            active-text="启用"
            inactive-text="禁用"
            :disabled="!permission.has('edit')"
            @change="crud.changeStatusWithNum(scope.row, scope.row.status, 'value')"
          />
        </template>
      </el-table-column>

      <el-table-column prop="createTime" label="创建时间" :show-overflow-tooltip="true" header-align="center" sortable="custom" />
      <el-table-column fixed="right" label="操作" width="120" header-align="center">
        <template slot-scope="scope">
          <udOperation
            :data="scope.row"
            :permission="permission"
          />
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页插件, 如果不需要注释掉下面一行,以及引入的 pagination 组件即可 -->
    <pagination />
    <!-- 表单 -->
    <SysDictForm />
    <!-- 详情 -->
    <!--<SysDictInfo />-->
  </div>
</template>

<script>

import crudApi, { page } from '@/api/sys/Dict'
import SysDictForm from './form'
// import SysDictInfo from './info'
import CRUD, { presenter, header } from '@crud/crud'
import CrudOpts from '@crud/CRUD.operation'
import rrOperation from '@crud/RR.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import Treeselect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { tree } from '@/api/sys/DictGroup'
// 系统字典
export default {
  components: {
    CrudOpts,
    rrOperation,
    udOperation,
    pagination,
    // SysDictInfo,
    SysDictForm,
    Treeselect
  },
  cruds() {
    return CRUD({ title: '字典', initDataMethod: page, crudMethod: { ...crudApi }})
  },
  mixins: [presenter(), header()],
  data() {
    return {
      permission: new Map(),
      normalizer(node) {
        // 去掉children=[]的children属性 respectively
        if (node.children && !node.children.length) {
          delete node.children
        }
        if (node.name) {
          node.label = node.name
        }
      },
      dictGroupTree: []
    }
  },
  mounted() {
    this.permission = this.$store.state.menu.buttonPermission.get('SysDict')
    this.getdictGroupTree()
  },
  methods: {
    [CRUD.HOOK.beforeRefresh]() {
      this.$emit('selectedChanged', '', '')
      return true
    },
    // 选中一个菜单
    handleCurrentChange(row) {
      this.$emit('selectedChanged', row.id, row.value, row.dictLabel)
    },
    // 获取字典分组结构树
    getdictGroupTree() {
      tree().then(res => {
        if (res && res.data) {
          this.dictGroupTree = res.data
        }
      })
    }
  }
}
</script>

<style >
  .vue-treeselect__control{
    height: 30px;
  }
  .vue-treeselect__placeholder, .vue-treeselect__single-value{
    line-height: 28px;
    font-size: small;
  }
  .vue-treeselect__menu{
    font-size: small;
  }
</style>
